package com.test.demo.service.impl;

import com.test.demo.mapper.EnqueueMapper;
import com.test.demo.mapper.TeamMapper;
import com.test.demo.po.Enqueue;
import com.test.demo.po.EnqueueKey;
import com.test.demo.po.Team;
import com.test.demo.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Cenoo on 2017/7/4.
 */

@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private EnqueueMapper enqueueMapper;

    @Transactional
    @Override
    public int addTeam(Team team) throws Exception {
        teamMapper.insert(team);
        Enqueue enqueue = new Enqueue();
        enqueue.setTeamId(team.getTeamId());
        enqueue.setTime(new Date().getTime());
        enqueue.setCaptaionFlag((byte) 1);
        enqueue.setUserId(team.getCaptainId());
        return enqueueMapper.insert(enqueue);
    }

    @Transactional
    @Override
    public int requestToEnqueue(int userId, int teamId) throws Exception {
        Team team = teamMapper.selectByPrimaryKey(teamId);
        if (team == null)
            return Team.STATUS_NOT_EXIST;
        if (team.getMaxPeople() <= team.getCountPeople())
            return Team.STATUS_FULL;
        Enqueue enqueue = new Enqueue();
        enqueue.setUserId(userId);
        enqueue.setTeamId(teamId);
        enqueue.setCaptaionFlag((byte) 0);
        enqueue.setTime(new Date().getTime());
        enqueueMapper.insert(enqueue);
        teamMapper.addTeamMember(teamId);
        return Team.STATUS_SUCCESS;
    }

    @Transactional
    @Override
    public int exitTeam(int userId, int teamId) throws Exception {
        EnqueueKey enqueueKey = new EnqueueKey();
        enqueueKey.setTeamId(teamId);
        enqueueKey.setUserId(userId);
        Enqueue enqueue = enqueueMapper.selectByPrimaryKey(enqueueKey);
        if (enqueue != null) {
            if (enqueue.getCaptaionFlag() > 0) {
                breakRanks(teamId, userId);
                return Team.STATUS_BREAKED;
            }
            enqueueMapper.deleteByPrimaryKey(enqueueKey);
            teamMapper.decreaseTeamMember(teamId);
        }
        return Team.STATUS_SUCCESS;
    }

    @Transactional
    @Override
    public int kickOut(int captainId, int userId, int teamId) throws Exception {
        EnqueueKey captainKey = new EnqueueKey();
        captainKey.setUserId(captainId);
        captainKey.setTeamId(teamId);
        Enqueue captainEnqueue = enqueueMapper.selectByPrimaryKey(captainKey);
        if (captainEnqueue != null && captainEnqueue.getCaptaionFlag() > 0) {
            EnqueueKey memberKey = new EnqueueKey();
            memberKey.setTeamId(teamId);
            memberKey.setUserId(userId);
            enqueueMapper.deleteByPrimaryKey(memberKey);
            teamMapper.decreaseTeamMember(teamId);
        }
        return Team.STATUS_SUCCESS;
    }

    @Transactional
    @Override
    public int breakRanks(int captainId, int teamId) throws Exception {
        List<Enqueue> enqueues = enqueueMapper.selectByTeamId(teamId);
        for (Enqueue enqueue : enqueues) {
            enqueueMapper.deleteByPrimaryKey(enqueue);
        }
        teamMapper.deleteByPrimaryKey(teamId);
        return Team.STATUS_SUCCESS;
    }

    @Override
    public List<Team> getAllTeams() throws Exception {
        return teamMapper.getAllTeams();
    }

    @Override
    public List<Team> searchTeam(String keyword) throws Exception {
        return teamMapper.searchTeam(keyword);
    }
}
